package cn.k7g.alloy.mybatis.typehandler;



import cn.k7g.alloy.model.SortOrder;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Optional;

/**
 * mybatis支持
 * @author victor-wu
 * @date 2021/9/27 上午11:51
 */

@MappedJdbcTypes(JdbcType.BIGINT)
@MappedTypes({SortOrder.class})
public class SortOrderTypeHandler extends BaseTypeHandler<SortOrder> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, SortOrder parameter, JdbcType jdbcType) throws SQLException {
        ps.setLong(i, parameter.longValue());
    }

    @Override
    public SortOrder getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return Optional.ofNullable(rs.getLong(columnName)).map(value -> new SortOrder(value)).orElse(null);
    }

    @Override
    public SortOrder getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return Optional.ofNullable(rs.getLong(columnIndex)).map(value -> new SortOrder(value)).orElse(null);
    }

    @Override
    public SortOrder getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return Optional.ofNullable(cs.getLong(columnIndex)).map(value -> new SortOrder(value)).orElse(null);
    }
}
